package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SeckillGoodsMapper;
import com.ruoyi.system.domain.SeckillGoods;
import com.ruoyi.system.service.ISeckillGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-09
 */
@Service
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements ISeckillGoodsService {
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;
    @Autowired
    private RedisCache redisCache;


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SeckillGoods selectSeckillGoodsById(Long id) {
        return seckillGoodsMapper.selectSeckillGoodsById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param seckillGoods 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SeckillGoods> selectSeckillGoodsList(SeckillGoods seckillGoods) {
        //先从内存中读取，没有再查数据库
//        List<SeckillGoods> list = redisCache.getCacheObject(Constants.SECKILL_GOOD_LIST);
//        if(CollectionUtils.isEmpty(list)){
//            list = seckillGoodsMapper.selectSeckillGoodsList(seckillGoods);
//            redisCache.setCacheObject(Constants.SECKILL_GOOD_LIST, list);
//        }
//        return list;
        return seckillGoodsMapper.selectSeckillGoodsList(seckillGoods);
    }

    @Override
    public AjaxResult refrestSeckillStock(SeckillGoods seckillGoods) {
        List<SeckillGoods> list = seckillGoodsMapper.selectSeckillGoodsList(seckillGoods);
        for (SeckillGoods goods : list) {
            String stockKey = Constants.SECKILL_GOOD + goods.getId() + Constants.SECKILL_GOOD_STOCK;
            redisCache.setCacheObject(stockKey, goods.getStockCount().intValue());
        }
        return AjaxResult.success();
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param seckillGoods 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSeckillGoods(SeckillGoods seckillGoods) {
            return seckillGoodsMapper.insertSeckillGoods(seckillGoods);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param seckillGoods 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSeckillGoods(SeckillGoods seckillGoods) {
        return seckillGoodsMapper.updateSeckillGoods(seckillGoods);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSeckillGoodsByIds(Long[] ids) {
        return seckillGoodsMapper.deleteSeckillGoodsByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSeckillGoodsById(Long id) {
        return seckillGoodsMapper.deleteSeckillGoodsById(id);
    }
}
